use "$base_out/Stallings_Matching.dta", clear
merge 1:1  school_id id_escuela using "$base_out/Sisat_Matching.dta"
drop _merge
gen Attended=0  if sis_participo_11_39!=. | obs_participo_11_39!=. | sis_participo_40!=. | obs_participo_40!=.
replace Attended=1 if (sis_participo_11_39==1 | obs_participo_11_39==1) | sis_participo_40==1 | obs_participo_40==1


gen Attended2=0 if  sis_participo_11_39!=. | obs_participo_11_39!=. | sis_participo_40!=. | obs_participo_40!=.
replace Attended2=1 if sis_participo_40==1 | obs_participo_40==1

*********************
**Base de controles**
*********************


merge 1:1 school_id using "$basein/Datos adicionales/base_Contraste.dta", gen(planea)

*keep if planea==3 | planea==1

**********************
***Base experimento***
**********************

merge 1:1 school_id using "$basein/Datos adicionales/Experimento_id.dta", gen(experimento)

*keep if experimento==3 | experimento==1

******************
***Base estrato***
******************

merge 1:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estratos)

*keep if _estratos==3 | _estratos==1

******************
***Base DWMS***
******************

merge 1:1 school_id using "$base_out/DWMS_Regresiones", gen(_DWMS)
*keep if _DWMS==3 | _DWMS==1


***Elimino secundarias***

drop if secondary==1
drop if treatgroup2=="Control"
drop if treatgroup2==""
drop if PEC_PLUS==.
***************************************************
***************************************************
*****Regresiones básicas indice y componentes******
***************************************************
***************************************************
drop Marg
gen Marg=(marginacion=="Muy alta o Alta") if !missing(marginacion)
foreach var of varlist urbana Marg  str insc_tot  ln_math25 ln_lang25 ln_mathlow25 ln_langlow25 lage lagep infra{
	gen miss_`var'=missing(`var')
	replace `var'=0 if missing(`var')
}
global varcontrol Marg##c.miss_Marg  c.urbana c.str c.insc_tot  c.ln_math25##c.miss_ln_math25 c.ln_lang25 c.ln_mathlow25 c.lage##c.miss_lage c.lagep c.infra



preserve
reg Attended $varcontrol i.estrato if PEC_PLUS==0
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

reg  management_sted3 propensity if PEC_PLUS==0
reg  management_sted3 propensity if PEC_PLUS==1

twoway (scatter management_sted3 propensity if PEC_PLUS==0, color("orange")) (lfit management_sted3 propensity if PEC_PLUS==0, color("orange")  lwidth("medthick") lpattern(dash)) (scatter management_sted3 propensity if PEC_PLUS==1, color("ebblue")) (lfit management_sted3 propensity if PEC_PLUS==1, color("ebblue") lwidth("medthick") lpattern(dash)),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
graph export "$graphs/Propensity_Management.pdf", replace 
restore

preserve
probit Attended $varcontrol i.estrato if PEC_PLUS==0
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

reg  management_sted3 propensity if PEC_PLUS==0
reg  management_sted3 propensity if PEC_PLUS==1

twoway (scatter management_sted3 propensity if PEC_PLUS==0, color("orange")) (lfit management_sted3 propensity if PEC_PLUS==0, color("orange") lwidth("medthick") lpattern(dash)) (scatter management_sted3 propensity if PEC_PLUS==1, color("ebblue")) (lfit management_sted3 propensity if PEC_PLUS==1, color("ebblue") lwidth("medthick") lpattern(dash)),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
	
graph export "$graphs/Propensity_Management_probit.pdf", replace 
restore

preserve
logit Attended $varcontrol i.estrato if PEC_PLUS==0
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

reg  management_sted3 propensity if PEC_PLUS==0
reg  management_sted3 propensity if PEC_PLUS==1

twoway (scatter management_sted3 propensity if PEC_PLUS==0, color("orange")) (lfit management_sted3 propensity if PEC_PLUS==0, color("orange") lwidth("medthick") lpattern(dash)) (scatter management_sted3 propensity if PEC_PLUS==1, color("ebblue")) (lfit management_sted3 propensity if PEC_PLUS==1, color("ebblue") lwidth("medthick") lpattern(dash)),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
	
graph export "$graphs/Propensity_Management_logit.pdf", replace 
restore





probit Attended $varcontrol i.estrato  if PEC_PLUS==0 & treatgroup!="Control" & state!=10, asis
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

logit Attended $varcontrol i.estrato if PEC_PLUS==0 & treatgroup!="Control" & state!=10, asis
predict propensity2
replace propensity2=0 if propensity2<=0 & !missing(propensity2)
replace propensity2=1 if propensity2>=1 & !missing(propensity2)

reg Attended $varcontrol i.estrato if PEC_PLUS==0 & treatgroup!="Control" & state!=10
predict propensity3
replace propensity3=0 if propensity3<=0 & !missing(propensity3)
replace propensity3=1 if propensity3>=1 & !missing(propensity3)

preserve
eststo clear
eststo : reghdfe  management_sted3 PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
eststo : reghdfe  management_sted3 c.propensity##c.PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
replace propensity=propensity2
eststo : reghdfe  management_sted3 c.propensity##c.PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
replace propensity=propensity3
eststo : reghdfe  management_sted3 c.propensity##c.PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)

label var propensity "P(Attending training)"
label var PEC_PLUS "Direct training"



esttab using "$tables/Propensity_Valora_Management.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(*PEC_PLUS* *propensity*) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	
restore	
	
label var Attended "Attended \$>\$ 10 hrs of training"
label var Attended2 "Attended all trainings"

eststo clear
eststo : ivreghdfe management_sted3 (Attended=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)
eststo : ivreghdfe management_sted3 (Attended2=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)
merge 1:m school_id using "$base_out/alum_pb_2018_modified", gen(_planea) update
eststo : ivreghdfe prom_pca (Attended=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)
eststo :  ivreghdfe prom_pca (Attended2=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)

esttab using "$tables/IV_Valora_Management.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(Attended Attended2) ///
	stats(N  Ftest, fmt(%9.0gc %9.2gc) ///
labels("N. of obs." "F test (first stage)"))




/*
reghdfe  prom_pca c.propensity##c.PEC_Plus if treatgroup!="Control" & state!=10, a(estrato)  vce(cluster cct_num)
gen tag=e(sample)
reghdfe  prom_pca PEC_Plus if  tag & treatgroup!="Control" & state!=10, a(estrato)  vce(cluster cct_num)

collapse (mean) prom_pca propensity PEC_Plus (count) N=prom_pca, by(school_id)
twoway (scatter prom_pca propensity if PEC_Plus==0, color("orange")) (lfit prom_pca propensity [aw=N] if PEC_Plus==0, color("orange")) (scatter prom_pca propensity if PEC_Plus==1, color("ebblue")) (lfit prom_pca propensity [aw=N] if PEC_Plus==1, color("ebblue")),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
*/
